package com.njlime.cms.service.sys;

import com.njlime.cms.entities.Label;
import com.njlime.cms.repository.sys.LabelDao;
import com.njlime.core.exception.BusinessException;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by Dean on 2017-8-10.
 */
@Service
@Transactional(readOnly = true)
public class LabelService {

    @Resource
    private LabelDao labelDao;

    /**
     * 详情
     * @param id
     * @return
     */
    public Label findOne(int id) {
        return labelDao.findOne(id);
    }

    /**
     * 保存
     * @param label
     * @return
     */
    @Transactional(readOnly = false)
    public int save(Label label) {
        Label ori = labelDao.getByName(label.getName(),label.getUnit_id());
        if(ori != null){
            throw new BusinessException("标签名称已存在");
        }
        label.setCreate_time(new Date());
        label.setIs_del(GlobalValue.NO);
        labelDao.save(label);
        return label.getId();
    }

    /**
     * 编辑
     * @param label
     * @return
     */
    @Transactional(readOnly = false)
    public int editLabel(Label label) {
        Label ori = labelDao.getByName(label.getName(),labelDao.findOne(label.getId()).getUnit_id());
        if(ori != null && !ori.getId().equals(label.getId())){
            throw new BusinessException("标签名称已存在");
        }
        return labelDao.editLabel(label);
    }

    /**
     * 分页列表
     * @param pageBean
     * @param label
     * @return
     */
    public PageBean list(PageBean pageBean, Label label) {
        return labelDao.list(pageBean, label);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int del(int id) {
        return labelDao.del(id);
    }
}
